<?php
require_once(ROOT.'/core/yckit.version.php');
require_once(ROOT.'/core/yckit.config.php');
require_once(ROOT.'/core/yckit.function.php');
require_once(ROOT.'/core/yckit.db.php');
require_once(ROOT.'/core/yckit.template.php');
require_once(ROOT.'/core/yckit.init.php');




abstract class base{
	public $template;
	public $db;
	public $config;
	public $action;
	public $do;
	public $theme;
	public function compatible(){
		$this->set_theme();
		$this->init();
		$this->init_hook();
		session_start();
		header("content-type:text/html;charset=utf-8");
	}
	public function init(){
		$this->db=new db(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
		$this->template=new template();
		$this->template->db=&$this->db;
		$this->config=$this->get_config();
		$this->action=isset($_GET['action'])?trim($_GET['action']):'';
		$this->do=isset($_GET['do'])?trim($_GET['do']):'';
		$this->theme=read_cache('default_theme');
	}
	function get_config(){
		$array=array();
		$array['version']=$GLOBALS['version'];
		$result=$this->db->result("SELECT config_value FROM ".DB_PREFIX."config");
		if($result){
			foreach($result as $row){
				if(!empty($row['config_value'])){
					$array=array_merge($array,unserialize(base64_decode($row['config_value'])));
				}
			}
		}
		$modules=$this->get_modules();
		if($modules){
			foreach($modules as $dir){
				$config_base=ROOT.'/core/modules/'.$dir.'/config.base.php';
				if(file_exists($config_base)){
					$config_base=include($config_base);
					$array=array_merge($array,$config_base);
				}
			}
		}
		return $array;
	}
	public function set_interface(){
		$modules=$this->get_modules();
		if($modules){
			foreach($modules as $dir){
				if($dir!='global'){
					$file=ROOT.'/core/modules/'.$dir.'/interface.php';
					if(file_exists($file)){
						require_once $file;
					}
					unset($file);
				}
			}
		}
		unset($modules);
	}
	
	private function set_theme(){
		if(!file_exists(ROOT.'/core/compiles/default_theme.php')){
			if($handle=opendir(ROOT."/core/themes")){
				while(false!==($dir=readdir($handle))){
					if ($dir!="."&&$dir!=".."&&$dir!="admin"&&$dir!="index.htm"&&is_dir(ROOT.'/core/themes/'.$dir)&&file_exists(ROOT.'/core/themes/'.$dir.'/install.lock')){
						write_cache('default_theme',$dir);
					}
				}
				closedir($handle);
			}
			unset($handle);
		}
	}
	public function init_hook(){
		$plugins=$this->get_plugins();
		if($plugins){
			foreach($plugins as $dir){
				$hook=ROOT.'/core/plugins/'.$dir.'/template.php';
				if (is_file($hook)&&file_exists(ROOT.'/core/plugins/'.$dir.'/install.lock')){
					include $hook;
				}
			}
		}
		$modules=$this->get_modules();
		if($modules){
			foreach($modules as $dir){
				$hook=ROOT.'/core/modules/'.$dir.'/template.php';
				if (is_file($hook)&&!file_exists(ROOT.'/core/modules/'.$dir.'/install.php')){
					include $hook;
				}
			}
		}
		unset($modules);
	}
	public function add_hook($name,$content){
		if(!empty($GLOBALS['ui'][$name])){
			$GLOBALS['ui'][$name].=$content;
		}else{
			$GLOBALS['ui'][$name]=$content;
		}
	}
	public function set_hook($name){
		$plugins=$this->get_plugins();
		if($plugins){
			foreach($plugins as $dir){
				$hook=ROOT.'/core/plugins/'.$dir.'/hook.'.$name.'.php';
				if (is_file($hook)&&file_exists(ROOT.'/core/plugins/'.$dir.'/install.lock')){
					include $hook;
				}
			}
		}
	}

	public function get_header(){
		$this->template->template_dir=ROOT.'/core/themes/'.$this->theme;
		$modules=$this->get_modules();
		if($modules){
			foreach($modules as $dir){
				if($dir!='global'){
					$file=ROOT.'/core/modules/'.$dir.'/interface.header.php';
					if(file_exists($file)){
						require_once $file;
					}
				}
			}
		}
		unset($modules);
		$result=$this->db->result("SELECT * FROM ".DB_PREFIX."menu WHERE menu_status=1 AND parent_id=0 ORDER BY menu_sort ASC");
		if($result){
			$URI=substr(strrchr($_SERVER['REQUEST_URI'],'/'),1);
			$n=1;
			foreach($result as $row){
				$array[$row['menu_id']]['id']=$row['menu_id'];
				$array[$row['menu_id']]['name']=$row['menu_name'];
				$array[$row['menu_id']]['description']=$row['menu_description'];
				$array[$row['menu_id']]['target']=$row['menu_target'];
				$array[$row['menu_id']]['link']=$row['menu_link'];
				$array[$row['menu_id']]['sort']=$row['menu_sort'];
				$children=array();
				$children_result=$this->db->result("SELECT * FROM ".DB_PREFIX."menu WHERE menu_status=1 AND parent_id=".$row['menu_id']." ORDER BY menu_id ASC");
				if($children_result){
					foreach($children_result as $child){
						$children[$child['menu_id']]['id']=$child['menu_id'];
						$children[$child['menu_id']]['name']=$child['menu_name'];
						$children[$child['menu_id']]['description']=$child['menu_description'];
						$children[$child['menu_id']]['link']=$child['menu_link'];
						$children[$child['menu_id']]['target']=$child['menu_target'];
					}
				}
				$array[$row['menu_id']]['children']=$children;
				unset($children_result);
				if(!empty($URI)){
					if($URI==$row['menu_link']){
						$array[$row['menu_id']]['active']=true;
					}
				}
				$n++;
			}
		}
		unset($result);
		$this->template->in('menu',$array);
		$this->template->in('config',$this->config);
		$content=$this->template->fetch('header.php');
		return $content;
	}
	public function get_footer(){
		$this->template->template_dir=ROOT.'/core/themes/'.$this->theme;
		$modules=$this->get_modules();
		if($modules){
			foreach($modules as $dir){
				if($dir!='global'){
					$file=ROOT.'/core/modules/'.$dir.'/interface.footer.php';
					if(file_exists($file)){
						require_once $file;
					}
				}
			}
		}
		unset($modules);
		$this->template->in('config',$this->config);
		$content=$this->template->fetch('footer.php');
		return $content;
	}

	#获取模块名称
	public function get_modules(){
		$array=array();
		if(false===($array=read_cache('modules'))){
			if($handle=opendir(ROOT."/core/modules")){
				while(false!==($dir=readdir($handle))){
					if ($dir!="."&&$dir!=".."&&$dir!="index.htm"){
						if(!file_exists(ROOT."/core/modules/".$dir."/install.php")){
							$array[]=$dir;
						}
					}
				}
				closedir($handle);
			}
			write_cache('modules',$array);
		}
		return $array;
	}
	#获取模块名称
	public function get_plugins(){
		$array=array();
		if(false===($array=read_cache('plugins'))){
			if($handle=opendir(ROOT."/core/plugins")){
				while(false!==($dir=readdir($handle))){
					if ($dir!="."&&$dir!=".."&&$dir!="index.htm"){
						if(file_exists(ROOT."/core/plugins/".$dir."/install.lock")){
							$array[]=$dir;
						}
					}
				}
				closedir($handle);
			}
			write_cache('plugins',$array);
		}
		return $array;
	}
	#生成首页
	public function html_index(){
		$this->template->template_dir=ROOT.'/core/themes/'.$this->theme;
		$this->template->in("path",PATH);
		$this->template->in('header',$this->get_header());
		$this->template->in('footer',$this->get_footer());
		$this->set_interface();
		$this->set_hook('html.index');
		$modules=$this->get_modules();
		if($modules){
			foreach($modules as $dir){
				if($dir!='global'){
					$file=ROOT.'/core/modules/'.$dir.'/interface.index.php';
					if(file_exists($file)){
						require_once $file;
					}
				}
			}
		}
		unset($modules);
		$content=$this->template->fetch('index.php');
		echo ROOT.'/' . DOMAIN_INDEX;
		file_put_contents(ROOT.'/' . DOMAIN_INDEX,$content);
		unset($content);
	}

}
?>